package authorized

import (
	"encoding/json"
	"gitee.com/yangguoliang001/go-gin-vue3/QO"
	"gitee.com/yangguoliang001/go-gin-vue3/pkg/response"
	"gitee.com/yangguoliang001/go-gin-vue3/util/code"
	"github.com/gin-gonic/gin"
	"strconv"
)

// QueryAuthList 调用方列表
// @Summary 调用方列表
// @Description 调用方列表
// @Tags API.authorized
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param page query int true "第几页" default(1)
// @Param pageSize query int true "每页显示条数" default(10)
// @Param businessKey query string false "调用方key"
// @Param businessDeveloper query string false "调用方对接人"
// @Success 200 {object} response.TResponse{data=vo.AuthorizedListVO}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/authorized [get]
// @Security LoginToken
func (a *authorizedController) QueryAuthList(c *gin.Context) {
	var authReq QO.AuthorizedListReq
	if err := c.ShouldBind(&authReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}

	authListRes, err := a.authorizedService.QueryAuthList(c, authReq)
	j, _ := json.Marshal(authListRes)
	authListResMap := make(map[string]interface{})
	_ = json.Unmarshal(j, &authListResMap)

	if err == nil {
		response.Success(c, "获取auth数据成功", authListResMap)
	}
}

// AddAuthorized 新增调用方数据
// @Summary 新增调用方数据
// @Description 新增调用方数据
// @Tags API.authorized
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param businessKey query string false "调用方key"
// @Param businessDeveloper query string false "调用方对接人"
// @Param remark query string false "备注"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/authorized [post]
// @Security LoginToken
func (a *authorizedController) AddAuthorized(c *gin.Context) {
	var authReq QO.AddAuthorizedReq
	if err := c.ShouldBind(&authReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}
	err := a.authorizedService.AddAuthorized(c, authReq)
	if err == nil {
		response.Success(c, "新增数据成功", gin.H{})
	}
}

// UpdateAuthorized 更新调用方为启用/禁用
// @Summary 更新调用方为启用/禁用
// @Description 更新调用方为启用/禁用
// @Tags API.authorized
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param id formData int true "id"
// @Param used formData int true "是否启用 1:是 -1:否"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/authorized/used [patch]
// @Security LoginToken
func (a *authorizedController) UpdateAuthorized(c *gin.Context) {
	var authReq QO.UpdateAuthorizedReq
	if err := c.ShouldBind(&authReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}
	err := a.authorizedService.UpdateAuthorized(c, authReq)
	//fmt.Println("UpdateAuthorized", authReq)

	if err == nil {
		response.Success(c, "更新数据成功", gin.H{})
	}
}

// DeleteAuthorized 删除调用方
// @Summary 删除调用方
// @Description 删除调用方
// @Tags API.authorized
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/authorized/{id} [delete]
// @Security LoginToken
func (a *authorizedController) DeleteAuthorized(c *gin.Context) {
	id := c.Param("id")
	//fmt.Println("DeleteAuthorized", id)
	idInt, _ := strconv.Atoi(id)
	err := a.authorizedService.DeleteAuthorized(c, idInt)

	if err == nil {
		response.Success(c, "删除数据成功", gin.H{
			"id": id,
		})
	}
}
